package com.xul.crowd.controller;

import com.github.pagehelper.PageInfo;
import com.xul.crowd.entity.Role;
import com.xul.crowd.service.RoleService;
import com.xul.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author xul
 * @create 2021-07-24 14:02
 */

@Controller
public class RoleController {


    @Autowired
    private RoleService roleService;

    /**
     * 权限查询 模糊带分页
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    // @PreAuthorize("hasRole('部长')")
    @ResponseBody
    @RequestMapping("role/get/page/info.json")
    public ResultEntity<PageInfo<Role>> getPageInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                    @RequestParam(value = "keyword", defaultValue = "") String keyword) {
        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);
        return ResultEntity.successWithData(pageInfo);
    }

    /**
     * 添加角色role
     *
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping("role/save.json")
    public ResultEntity<String> saveRole(Role role) {
        roleService.saveRole(role);
        return ResultEntity.successWithData();
    }

    /**
     * 更新角色role
     *
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping("role/update.json")
    public ResultEntity<String> updateRole(Role role) {
        roleService.update(role);
        return ResultEntity.successWithData();
    }

    /**
     * 批量删除角色 包含单个删除
     * @param rodeIdList
     * @return
     */
    @ResponseBody
    @RequestMapping("role/remove/by/role/id/array.json")
    public ResultEntity<String> removeByRoleIdArray(@RequestBody List<Integer> rodeIdList) {
        roleService.removeRole(rodeIdList);
        return ResultEntity.successWithData();
    }
}
